<?php
/**
 * Parse JSON 
 */

$xview = Xview::instance();
$data = $xview->val('jsonData');
if(empty($data)) {
	$error = array('title' => "nothing to be indexed");
	echo json_encode($error);
	exit;
}

$status = new \Fox\Background\JsonStatusReporter("Generating Document Index",
	array('Starting Indexation','Finding new Words',
			'Adding Words to dictionnary',
			'Saving Matches',
			'Indexation Complete',)
);

$grouping_type = \Hive\Ressource::className();
\Fox\Background\Task::run($status, function ($status) use ($data,$grouping_type)  
{
	$toBeIndexed = json_decode(str_replace("'",'"',$data),true);
	$status->start();
	foreach($toBeIndexed as $toBeIndexedData) {
		try {
			$document = \Fox\Dao::load(\Fox\Document::className(),$toBeIndexedData['docid']);
			$searchEngine = new \Fox\SearchEngine();
			$searchEngine->add($document,
							$toBeIndexedData['rid'],
							$grouping_type,
							$status);
		
		} catch(\Fox\Object\Exception\NotExist $e) {
			\Xview::instance()->log($e->getMessage());
		} catch(\Exception $e) {
			\Xview::instance()->log($e->getMessage());
		}
	}
	$status->success();
});